#include "gzp.h"
#include "gzll.h"
#include <string.h>
#include <stdint.h>

// ==================================================================
/// @brief  : 更新nrf 跳频列表和通讯地址
/// @param  : system_address 系统通讯地址,地址长度为5个字节
// ==================================================================
void gzp_update_radio_params(const uint8_t* system_address)
{
    uint8_t channels[GZLL_MAX_CHANNEL_TAB_SIZE];
    gzll_set_address(HAL_NRF_PIPE0, (uint8_t const*)system_address);                              
    gzp_generate_channels(&channels[0], &system_address[0], gzll_get_channel_tab_size());           /*<通过系统地址来生成跳频频道*/   
    gzll_set_channels(&channels[0], gzll_get_channel_tab_size());                                   /*<将生成的跳频列表写入到gzll中,用来gzll跳频使用*/
}

// ==================================================================
/// @brief  : 生成跳频列表
/// @param  : ch_dst 频道缓存区
/// @param  : system_address 系统通讯地址,是根据地址来产生跳频频道
/// @param  : channel_tab_size 跳频频道大小
// ==================================================================
void gzp_generate_channels(uint8_t* ch_dst, const uint8_t* system_address, uint8_t channel_tab_size)
{
    uint8_t binsize, spacing, i;

    binsize = (GZP_CHANNEL_MAX - GZP_CHANNEL_MIN) / channel_tab_size;

    ch_dst[0] = GZP_CHANNEL_LOW;
    ch_dst[channel_tab_size - 1] = GZP_CHANNEL_HIGH;

    for (i = 1; i < (channel_tab_size - 1); i++) {
        ch_dst[i] = (binsize * i) + (system_address[i % 4] % binsize);  
    }

    for (i = 1; i < channel_tab_size; i++) {
        spacing = (ch_dst[i] - ch_dst[i - 1]); 
        if (spacing < GZP_CHANNEL_SPACING_MIN) {
            ch_dst[i] += (GZP_CHANNEL_SPACING_MIN - spacing); 
        }
    } 
}              
